package com.zhuoyue.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.zhuoyue.common.annotation.DataScope;

import com.zhuoyue.system.domain.*;
import com.zhuoyue.system.domain.dto.BaseDeviceMessage;
import com.zhuoyue.system.domain.po.TblLocalScenePo;
import com.zhuoyue.system.domain.vo.LocalSceneVo;
import com.zhuoyue.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhuoyue.system.service.ITblLocalSceneService;

/**
 * 本地场景Service业务层处理
 *
 * @author hewenze
 * @date 2020-08-10
 */
@Service
public class TblLocalSceneServiceImpl implements ITblLocalSceneService {
    @Autowired
    private TblLocalSceneMapper tblLocalSceneMapper;
    @Autowired
    private LocalSceneGatewayRelationMapper gatewayRelationMapper;
    @Autowired
    private LocalSceneZigbeeDeviceRelationMapper zigbeeDeviceRelationMapper;
    @Autowired
    private LocalSceneInfraredDeviceRelationMapper infraredDeviceRelationMapper;
    @Autowired
    private LocalSceneGroupRelationMapper groupRelationMapper;
    @Autowired
    private TblDeviceActionsMapper actionsMapper;


    /**
     * 查询本地场景列表
     *
     * @param tblLocalScenePo 本地场景
     * @return 本地场景
     */
    @Override
    @DataScope(deptAlias = "s_d")
    public List<LocalSceneVo> selectTblLocalSceneList(TblLocalScenePo tblLocalScenePo) {
        return tblLocalSceneMapper.selectTblLocalSceneList(tblLocalScenePo);
    }

    /**
     * 根据Id查询场景信息
     *
     * @param id
     * @return
     */
    @Override
    public LocalSceneVo selectById(String id) {
        LocalSceneVo localSceneVo = tblLocalSceneMapper.selectById(id);
        List<LocalSceneGatewayRelation> localSceneGatewayRelations = gatewayRelationMapper.selectByPrimarySceneId(id);
        List<LocalSceneZigbeeDeviceRelation> localSceneZigbeeDeviceRelations = zigbeeDeviceRelationMapper.selectByPrimarySceneId(id);
        List<LocalSceneInfraredDeviceRelation> localSceneInfraredDeviceRelations = infraredDeviceRelationMapper.selectByPrimarySceneId(id);
        List<LocalSceneGroupRelation> localSceneGroupRelations = groupRelationMapper.selectByPrimarySceneId(id);

        List<BaseDeviceMessage> baseDeviceMessages = new ArrayList<>(10);
        localSceneGatewayRelations.forEach(g -> {
            BaseDeviceMessage baseDeviceMessage = new BaseDeviceMessage(g.getGatewayName(), "网关", g.getGatewayMac(), g.getControlParam(), g.getStateDes());
            baseDeviceMessages.add(baseDeviceMessage);
        });

        localSceneZigbeeDeviceRelations.forEach(z -> {
            List<TblDeviceActions> tblDeviceActions = actionsMapper.listActionsByDeviceId(z.getDeviceId());
            BaseDeviceMessage baseDeviceMessage = new BaseDeviceMessage(z.getDeviceName(), z.getDeviceType(), z.getDefaultDeviceName(), z.getZigbeeMac(),
                    tblDeviceActions, z.getControlParam(), z.getStateDes());
            baseDeviceMessages.add(baseDeviceMessage);
        });

        localSceneInfraredDeviceRelations.forEach(i -> {
            BaseDeviceMessage baseDeviceMessage = new BaseDeviceMessage(i.getInfraredName(), "红外", "", i.getControlParam(), i.getStateDes());
            baseDeviceMessages.add(baseDeviceMessage);
        });
        localSceneGroupRelations.forEach(g -> {
            BaseDeviceMessage baseDeviceMessage = new BaseDeviceMessage(g.getGroupName(), "群组", "", g.getControlParam(), g.getStateDes());
            baseDeviceMessages.add(baseDeviceMessage);
        });
        localSceneVo.setDeviceList(baseDeviceMessages);
        return localSceneVo;
    }
}
